package com.insigma.business.qrcode.soldier.util;

import cn.hutool.core.codec.Base64;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5Utils {

    

    public static String encodeByMD5(String originStr) {

        String result = null;

        char hexDigits[] = {

                '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

        if(originStr != null){

            try {

                

                MessageDigest md = MessageDigest.getInstance("MD5");

                

                byte[] source = originStr.getBytes("utf-8");

                

                md.update(source);

                

                byte[] tmp = md.digest();

                

                char[] str = new char[32];

                for(int i=0,j=0; i < 16; i++){

                    

                    

                    

                    byte b = tmp[i];

                    

                    

                    

                    str[j++] = hexDigits[b>>>4 & 0xf];

                    

                    str[j++] = hexDigits[b&0xf];

                }

                result = new String(str);

            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }

        return result;
    }

    public static final String str2MD5(String _str) {
        char[] hexDigits = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

        try {
            byte[] strTemp = _str.getBytes();
            MessageDigest mdTemp = MessageDigest.getInstance("MD5");
            mdTemp.update(strTemp);
            byte[] md = mdTemp.digest();
            int j = md.length;
            char[] str = new char[j * 2];
            int k = 0;

            for(int i = 0; i < j; ++i) {
                byte byte0 = md[i];
                str[k++] = hexDigits[byte0 >>> 4 & 15];
                str[k++] = hexDigits[byte0 & 15];
            }

            return new String(str);
        } catch (Exception var10) {
            return null;
        }
    }


    public static void main(String[] args){;
        
        System.out.println(encodeByMD5("Admin123456"));
        String s1 = Base64.encode("loadmin".getBytes()) + "XAa3Eq0z9pSosVD1aDZJ" + encodeByMD5("Admin123456");
        String s2 = encodeByMD5(s1);
        System.out.println(s2);
    }

    public static String ecodeByMD5(String originstr) {
        String result = null;
        char[] hexDigits = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
        if (originstr != null) {
            try {
                MessageDigest md = MessageDigest.getInstance("MD5");
                byte[] source = originstr.getBytes("utf-8");
                md.update(source);
                byte[] tmp = md.digest();
                char[] str = new char[32];
                int i = 0;

                for(int var8 = 0; i < 16; ++i) {
                    byte b = tmp[i];
                    str[var8++] = hexDigits[b >>> 4 & 15];
                    str[var8++] = hexDigits[b & 15];
                }

                result = new String(str);
            } catch (NoSuchAlgorithmException var10) {
                var10.printStackTrace();
            } catch (UnsupportedEncodingException var11) {
                var11.printStackTrace();
            }
        }

        return result;
    }

}
